package com.yaodian100.ec.report.rank.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.rank.entity.ViewReportProductClick;
import com.yaodian100.ec.report.rank.service.ViewReportProductClickService;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.service.ShelfService;

public class ViewReportProductClickServiceImpl extends DomainServiceImpl<ViewReportProductClick> implements ViewReportProductClickService {
	
	private static final String NAMED_QUERY_PARAMETER_NAME_COMPUTE_DATE = "computeDate";
	private static final String NAMED_QUERY_PARAMETER_NAME_SECOND_CATEGORY_ID = "secondCategoryId";
	private static final String NAME_QUERY_VIEW_REPORT_PRODUCT_CLICK_GET_RANK_AFTER_DATE = "ViewReportProductClick.getRankAfterDate";
	
	@Autowired
	private ShelfService shelfService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ProductService productService;
	
	public int rankShortDays() {
		int result = Integer.parseInt(ServerValue.getMidRankShortDays());
		return result;
	}
	
	public int rankLongDays() {
		return Integer.parseInt(ServerValue.getMidRankLongDays());
	}
	
	public int mininumRecordSize() {
		return 20;
	}
	
	public List<ViewReportProductClick> getTopRank(String categoryId) {
		List<ViewReportProductClick> result = getTopRank(categoryId, rankShortDays());
		if (result.size() < mininumRecordSize()) {
			result = getTopRank(categoryId, rankLongDays());
		}
		return result;
	}

	public List<ViewReportProductClick> getTopRank(String categoryId, int retroDays){
		if (categoryId == null) {
			return null;
		}
		
		List<ViewReportProductClick> result = new ArrayList<ViewReportProductClick>();
		
		Date now = baseDate(retroDays);
		
		Map<String, Serializable> attrs = new HashMap<String, Serializable>();
		attrs.put(NAMED_QUERY_PARAMETER_NAME_SECOND_CATEGORY_ID, categoryId);
		attrs.put(NAMED_QUERY_PARAMETER_NAME_COMPUTE_DATE, now);
		List<Object[]> queryResult = getDao().getNameQuery(NAME_QUERY_VIEW_REPORT_PRODUCT_CLICK_GET_RANK_AFTER_DATE, attrs, 0, 50);
		
		logger.debug("result record count: " + new Integer(queryResult.size()).toString());
		for (Object[] resultItem: queryResult) {
			
			ViewReportProductClick displayRecord = new ViewReportProductClick();
			Object productIdObject = resultItem[0];
			if (productIdObject != null) {
				displayRecord.getPrimaryKey().setProductId(productIdObject.toString());
			}
			Object categoryIdObject = resultItem[1];
			if (categoryIdObject != null) {
				displayRecord.getPrimaryKey().setCategoryId(Long.parseLong(categoryIdObject.toString()));
			}
			Object productNameObject = resultItem[2];
			if (productNameObject != null) {
				displayRecord.setProductName(productNameObject.toString());
			}
			Object categoryNameObject = resultItem[3];
			if (categoryNameObject != null) {
				displayRecord.setCategoryName(categoryNameObject.toString());
			}
			Object secondCategoryIdObject = resultItem[4];
			if (secondCategoryIdObject != null) {
				displayRecord.setSecondCategoryId(secondCategoryIdObject.toString());
			}
			Object secondCategoryNameObject = resultItem[5];
			if (secondCategoryNameObject != null) {
				displayRecord.setSecondCategoryName(secondCategoryNameObject.toString());
			}
			Object sortObject = resultItem[6];
			if (sortObject != null) {
				displayRecord.setSort(Long.parseLong(sortObject.toString()));
			}
			Object clickObject = resultItem[7];
			if (clickObject != null) {
				displayRecord.setClick(Long.parseLong(clickObject.toString()));
			}
			
			Shelf shelf = getShelfService().getActiveShelfByProductId(displayRecord.getPrimaryKey().getProductId());
			if (shelf != null) {
				//要有上架才能列入排行
				displayRecord.setShelfPrice(shelf.getShelfPrice());
				result.add(displayRecord);
			}
			
		}
		
		return result;
	}
	
	public ViewReportProductClick createNewRecordFromProductId(String productId) {
		Product product = productService.get(productId);
		return createNewRecordFromProduct(product);
	}
	
	public ViewReportProductClick createNewRecordFromProduct(Product product) {
		if (product == null) {
			return null;
		}
		
		ViewReportProductClick result = new ViewReportProductClick();
		result.getPrimaryKey().setProductId(product.getProductId());
		result.setProductName(product.getProductName());
		
		Long categoryId = new Long(product.getMainCategoryID());
		result.getPrimaryKey().setCategoryId(categoryId);
		
		Category category = categoryService.get(categoryId);
		result.setCategoryName(category.getName());
		
		Shelf shelf = shelfService.getActiveShelfByProductId(product.getProductId());
		if (shelf != null) {
			result.setShelfPrice(shelf.getShelfPrice());
		}
		
		return result;
	}

	public ShelfService getShelfService() {
		return this.shelfService;
	}
	public void setShelfService(ShelfService shelfService) {
		this.shelfService = shelfService;
	}

	public ProductService getProductService() {
		return this.productService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	static Date baseDate(int retroDays) {
		Calendar nowCalendar = Calendar.getInstance();
		nowCalendar.add(Calendar.DATE, retroDays * -1);
		
		return nowCalendar.getTime();
	}
	
}
